package com.itheima.utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

@Component
public class JedisUtil {

    @Autowired
    JedisPool jedisPool;

    public void sadd(String key, String value){
        Jedis resource = null;
        try{
            resource = jedisPool.getResource();
            resource.sadd(key,value);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if(null != resource){
                resource.close();
            }
        }
    }

    public void addTimeLimited(String key, int second, String value){
        Jedis resource = null;
        try{
            resource = jedisPool.getResource();
            resource.setex(key,second,value);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if(null != resource){
                resource.close();
            }
        }
    }

    public String get(String key){
        Jedis resource = null;
        try{
            resource = jedisPool.getResource();
            return resource.get(key);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if(null != resource){
                resource.close();
            }
        }
        return "";
    }

    public long delete(String key){
        Jedis resource = null;
        try{
            resource = jedisPool.getResource();
            return resource.del(key);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if(null != resource){
                resource.close();
            }
        }
        return 0L;
    }
}